<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="手机号" prop="age">
    <el-input v-model.number="ruleForm.age"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  <!-- 验证码 -->
 <div><input type="text" name="" id="" v-model="yanzm"></div> <yz @click="yzm" :identifyCode="identifyCode"></yz>
      <input type="text" value="获取手机验证码">
            
       <div> <el-checkbox v-model="checked">备选项</el-checkbox></div>
 
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </div>
</template>
<script>
import yanz from "@/components/lxc/yanzhengma.vue";
import axios from "axios";
  export default {
  components: {
    yz:yanz,
  },
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机号不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入正确的手机号'));
          } else {
             const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
            if (!reg.test(value)) {
              callback(new Error('手机号各式错误'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        yanzm:'',
        identifyCode:'',
         checked: true,
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    mounted(){
      for(var i=0;i<4;i++){
      this.identifyCode=this.identifyCode+ Math.floor(Math.random() * (10 - 1 + 1) + 0);
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
               if(this.checked){
              if(this.yanzm==this.identifyCode){
                  var url = "http://localhost:3000";
                    axios.post(url + "/login/useradd",{id:this.ruleForm.age,pass:this.ruleForm.pass})
                        .then(response => {
                         window.console.log('插入成功'+response)
                        })
                        .then(() => {
                        });
                 window.console.log(this.ruleForm.age+'+++'+this.ruleForm.pass);
              }else{
                alert('验证码不正确')
              }
            }else{
              alert('请点击阅读选框')
            }




         
          } else {
           window.console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      
      yzm(){
      }
    }
  }
</script>